home *** CD-ROM | disk | FTP | other *** search
- Path: xanth!mcnc!gatech!ukma!cwjcc!hal!ncoast!allbery
- From: ljz@fxgrp.UUCP (Lloyd Zusman)
- Newsgroups: comp.sources.misc
- Subject: v04i114: Arbitrary Precision Math Library -- 3 of 5
- Message-ID: <8810051944.AA05722@fxgrp.fx.com>
- Date: 7 Oct 88 00:10:23 GMT
- Sender: allbery@ncoast.UUCP
- Reply-To: ljz@fxgrp.UUCP (Lloyd Zusman)
- Lines: 1187
- Approved: allbery@ncoast.UUCP
-
- Posting-number: Volume 4, Issue 114
- Submitted-by: "Lloyd Zusman" <ljz@fxgrp.UUCP>
- Archive-name: apml/Part3
-
- Enclosed you will find the Arbitrary Precision Math Library (3 of 5)
-
- Please post this to the comp.sources.misc newsgroup.
-
- I finally got this into good enough shape to send out to the net. To use,
- just unshar the 5 pieces, read the README file, possibly alter the makefiles
- to conform to your system's conventions, and then type 'make test'.
-
- Good luck!
-
- --
- Lloyd Zusman Internet: ljz@fx.com
- Master Byte Software or ljz%fx.com@ames.arc.nasa.gov
- Los Gatos, California or fxgrp!ljz@ames.arc.nasa.gov
- "We take things well in hand." uucp: ...!ames!fxgrp!ljz
- [ our Internet connection is down: use uucp or mail to the entry above it ]
-
- #--------------------------Cut Here--------------------------
- #! /bin/sh
- # This is a shell archive. Remove anything before the "#! /bin/sh" line,
- # then unpack it by saving it in a file and typing "sh file."
- #
- # Wrapped by Lloyd Zusman (ljz) at fxgrp on Wed Oct 5 12:41:51 1988
- #
- # unpacks with default permissions
- #
- # Contents : memory.c misc.c
- #
- if `test ! -s memory.c`
- then
- echo "x - memory.c"
- sed 's/^X//' > memory.c << '@\END_OF_FILE_memory.c'
- X/******************************************************************************
- X
- X Arbitrary Precision Math Library General Public License
- X (Written October 5, 1988)
- X
- X Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
- X Gatos, California. Everyone is permitted to copy and distribute
- X verbatim copies of this license, but changing it is not allowed.
- X You can also use this wording to make the terms for other programs.
- X
- X The wording of this license is based on that of the
- X "GNU EMACS GENERAL PUBLIC LICENSE" by Richard Stallman,
- X Copyright (C) 1985, 1987, 1988, version of February 11, 1988,
- X but since some of the text has been changed, please be sure to
- X READ THIS CAREFULLY!
- X
- X This general public license is intended to give everyone the right
- Xto share the Arbitrary Precision Math Library (hereinafter referred to
- Xas the "APM Library"). To make sure that you get the rights we want
- Xyou to have, I need to make restrictions that forbid anyone to deny
- Xyou these rights or to ask you to surrender the rights.
- X
- X Specifically, we want to make sure that you have the right to give
- Xaway copies of the APM Library, that you receive source code or else
- Xcan get it if you want it, that you can change the APM Library or use
- Xpieces of it in new programs, and that you know you can do these
- Xthings.
- X
- X To make sure that everyone has such rights, we have to forbid you to
- Xdeprive anyone else of these rights. For example, if you distribute
- Xcopies of the APM Library, you must give the recipients all the
- Xrights that you have. You must make sure that they, too, receive or
- Xcan get the source code. And you must tell them their rights.
- X
- X Also, for our own protection, we must make certain that everyone
- Xfinds out that there is no warranty for the APM Library. If the APM
- XLibrary is modified by someone else and passed on, we want its
- Xrecipients to know that what they have is not what we distributed, so
- Xthat any problems introduced by others will not reflect on our
- Xreputation.
- X
- X Therefore we (Lloyd Zusman and Master Byte Software) make the
- Xfollowing terms which say what you must do to be allowed to
- Xdistribute or change the APM Library.
- X
- X COPYING POLICIES
- X
- X1. You may copy and distribute verbatim copies of the APM Library
- Xsource code as you receive it, in any medium, provided that you
- Xconspicuously and appropriately publish on each copy a valid copyright
- Xnotice "Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
- XGatos, California" (or with whatever year is appropriate); keep intact
- Xthe notices on all files that refer to this License Agreement and to
- Xthe absence of any warranty; and give any other recipients of the the
- XAPM Library program a copy of this License Agreement along with the
- Xprogram. You may charge a distribution fee for the physical act of
- Xtransferring a copy.
- X
- X 2. You may modify your copy or copies of the APM Library source code or
- Xany portion of it, and copy and distribute such modifications under
- Xthe terms of Paragraph 1 above, provided that you also do the following:
- X
- X a) cause the modified files to carry prominent notices stating
- X that you changed the files and the date of any change; and
- X
- X b) cause the whole of any work that you distribute or publish, that in
- X whole or in part contains or is a derivative of the APM Library or any
- X part thereof, to be licensed to all third parties on terms identical
- X to those contained in this License Agreement (except that you may
- X choose to grant more extensive warranty protection to some or all
- X third parties, at your option).
- X
- X c) You may charge a distribution fee for the physical act of
- X transferring a copy, and you may at your option offer warranty
- X protection in exchange for a fee.
- X
- X d) You may not charge a license fee for the whole of any work that
- X you distribute or publish, that in whole or in part contains or is
- X a derivative of the APM library or any part thereof, without the
- X express written permission of Lloyd Zusman and Master Byte Software;
- X whether this permission is granted for free or in return for goods
- X services, royalties, or other compensation will be determined
- X solely by Lloyd Zusman and Master Byte Software.
- X
- XMere aggregation of another unrelated program with this program (or its
- Xderivative) on a volume of a storage or distribution medium does not bring
- Xthe other program under the scope of these terms.
- X
- X 3. You may copy and distribute the APM Library (or a portion or
- Xderivative of it, under Paragraph 2) in object code or executable form
- Xunder all the terms of Paragraphs 1 and 2 above provided that you also
- Xdo one of the following:
- X
- X a) accompany it with the complete corresponding machine-readable
- X source code, which must be distributed under the terms of
- X Paragraphs 1 and 2 above; or,
- X
- X b) accompany it with a written offer, valid for at least three
- X years, to give any third party free (except for a nominal
- X shipping charge) a complete machine-readable copy of the
- X corresponding source code, to be distributed under the terms of
- X Paragraphs 1 and 2 above; or,
- X
- X c) accompany it with the information you received as to where the
- X corresponding source code may be obtained. (This alternative is
- X allowed only for noncommercial distribution and only if you
- X received the program in object code or executable form alone.)
- X
- XFor an executable file, complete source code means all the source code
- Xfor all modules it contains; but, as a special exception, it need not
- Xinclude source code for modules which are standard libraries that
- Xaccompany the operating system on which the executable file runs.
- X
- X 4. You may not copy, sublicense, distribute or transfer the APM
- XLibrary except as expressly provided under this License Agreement.
- XAny attempt otherwise to copy, sublicense, distribute or transfer the
- XAPM Library is void and your rights to use the APM Library under this
- XLicense agreement shall be automatically terminated. However, parties
- Xwho have received computer software programs from you with this
- XLicense Agreement will not have their licenses terminated so long as
- Xsuch parties remain in full compliance.
- X
- X 5. If you wish to incorporate parts of the APM Library into other
- Xprograms whose distribution conditions are different, write to Lloyd
- XZusman at Master Byte Software. We have not yet worked out a simple
- Xrule that can be stated here, but we will often permit this. We will
- Xbe guided by the goals of (1) preserving the free status of all
- Xderivatives of our free software; of (2) promoting the sharing and
- Xreuse of software; and of (3) not allowing anyone to profit from the
- Xuse of our software without us also having the opportunity to share
- Xin these profits.
- X
- XYour comments and suggestions about our licensing policies and our
- Xsoftware are welcome! Please contact Lloyd Zusman, Master Byte
- XSoftware, 127 Wilder Ave., Los Gatos, California 95030, or call
- X(408) 395-5693.
- X
- X NO WARRANTY
- X
- X BECAUSE THE APM LIBRARY IS LICENSED FREE OF CHARGE, WE PROVIDE
- XABSOLUTELY NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE
- XLAW. EXCEPT WHEN OTHERWISE STATED IN WRITING, MASTER BYTE SOFTWARE,
- XLLOYD ZUSMAN AND/OR OTHER PARTIES PROVIDE THE APM LIBRARY "AS IS"
- XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
- XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- XFITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
- XAND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE THE APM
- XLIBRARY PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
- XSERVICING, REPAIR OR CORRECTION.
- X
- X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL MASTER BYTE
- XSOFTWARE, LLOYD ZUSMAN, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND
- XREDISTRIBUTE THE APM LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
- XDAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL,
- XINCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
- XINABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA
- XBEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A
- XFAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY
- XMASTER BYTE SOFTWARE) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF
- XTHE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
- X
- X******************************************************************************/
- X
- X
- X/*
- X * Memory allocation routines for the APM library.
- X *
- X * $Log: memory.c,v $
- X * Revision 1.0 88/10/05 12:38:13 ljz
- X * Initial release.
- X *
- X */
- X#ifndef lint
- Xstatic char rcsid[] = "$Header: memory.c,v 1.0 88/10/05 12:38:13 ljz Exp $";
- X#endif /* ! lint */
- X
- X#include <stdio.h>
- X#include "apm.h"
- X#include "apmlocal.h"
- X
- Xtypedef struct {
- X APM apm;
- X unsigned int flags;
- X} APMMEMTAB;
- X
- X#define MEMAVAIL 1
- X
- Xstatic APMMEMTAB *APM_memtab = (APMMEMTAB *)NULL;
- Xstatic int APM_memnum = 0;
- X
- X#ifndef MSC
- Xextern char *malloc();
- Xextern char *realloc();
- Xextern void free();
- X#endif /* MSC */
- X
- Xchar *
- XAPM_alloc_mem(mem, nelem, elsize)
- Xchar *mem;
- Xunsigned int nelem;
- Xunsigned int elsize;
- X{
- X char *cp;
- X unsigned int size;
- X
- X apm_errno = APM_OK;
- X
- X if (elsize == 0) {
- X elsize = 1; /* default to 1 for ease of use */
- X }
- X
- X if (nelem == 0) { /* malloc(0) & realloc(MEM, 0) not portable */
- X nelem = 1;
- X }
- X
- X size = nelem * elsize;
- X if (size < nelem || size < elsize) { /* overflow */
- X (void)APM_set_errno(APM_EPARM);
- X return (NULL);
- X }
- X
- X if (mem == NULL) {
- X cp = malloc(size);
- X }
- X else {
- X cp = realloc(mem, size);
- X }
- X
- X if (cp == NULL) {
- X (void)APM_set_errno(APM_ENOMEM);
- X }
- X
- X return (cp);
- X}
- X
- Xint
- XAPM_free_mem(mem)
- Xchar *mem;
- X{
- X if (mem == NULL) {
- X return (APM_set_errno(APM_WNULL));
- X }
- X free(mem);
- X
- X return (APM_OK);
- X}
- X
- XAPM
- XAPM_do_alloc()
- X{
- X APM apm;
- X
- X apm_errno = APM_OK;
- X
- X apm = (APM)APM_alloc_mem(NULL, 1, sizeof (struct apm_struct));
- X if (apm == (APM)NULL) {
- X APM_set_errno(APM_ENOMEM);
- X return ((APM)NULL);
- X }
- X apm->magic = APM_MAGIC;
- X apm->sign = 1;
- X apm->base = 0;
- X apm->length = 0;
- X apm->dp = 0;
- X apm->alloclen = 0;
- X apm->data = NULL;
- X
- X return (apm);
- X}
- X
- XAPM
- XAPM_alloc()
- X{
- X APM apm;
- X int i;
- X int slot = -1;
- X
- X /*
- X * Go through allocation table to see if we have an available
- X * APM value. If so, set its state to "not available" and then
- X * return it.
- X */
- X for (i = 0; i < APM_memnum; ++i) {
- X if ((APM_memtab[i].flags & MEMAVAIL) != 0) {
- X if (APM_memtab[i].apm != (APM)NULL) {
- X APM_memtab[i].flags = 0;
- X apm = APM_memtab[i].apm;
- X apm->magic = APM_MAGIC;
- X apm->sign = 1;
- X apm->base = 0;
- X apm->length = 0;
- X apm->dp = 0;
- X for (i = 0; i < apm->alloclen; ++i) {
- X apm->data[i] = 0;
- X }
- X return (apm);
- X }
- X }
- X }
- X
- X /*
- X * If we're here, nothing is available. We now see if there is
- X * an empty slot.
- X */
- X for (i = 0; i < APM_memnum; ++i) {
- X if (APM_memtab[i].apm == (APM)NULL) {
- X slot = i;
- X break;
- X }
- X }
- X
- X /*
- X * If we're here, we couldn't find an unused, pre-allocated value.
- X * If there's an available slot, allocate a value; otherwise,
- X * increase the size of the table and create a slot.
- X */
- X if (slot < 0) {
- X APMMEMTAB *mt;
- X int newmem = APM_memnum;
- X slot = newmem;
- X newmem += TABINCR;
- X mt = (APMMEMTAB *)APM_alloc_mem(APM_memtab, newmem,
- X sizeof (APMMEMTAB));
- X if (mt == (APMMEMTAB *)NULL) {
- X APM_set_errno(APM_ENOMEM);
- X return ((APM)NULL);
- X }
- X APM_memtab = mt;
- X APM_memnum = newmem;
- X for (i = slot; i < APM_memnum; ++i) {
- X APM_memtab[i].flags = 0;
- X APM_memtab[i].apm = (APM)NULL;
- X }
- X }
- X
- X /*
- X * We now can allocate the new APM value.
- X */
- X apm = APM_do_alloc();
- X if (apm != (APM)NULL) {
- X APM_memtab[slot].flags = 0;
- X APM_memtab[slot].apm = apm;
- X }
- X
- X return (apm);
- X}
- X
- Xint
- XAPM_do_free(apm)
- XAPM apm;
- X{
- X int ercode;
- X
- X apm_errno = APM_OK;
- X
- X if (apm == (APM)NULL) {
- X return (APM_WNULL);
- X }
- X
- X apm->magic = 0L;
- X
- X if (apm->data != NULL && apm->alloclen > 0) {
- X ercode = APM_free_mem((char *)apm->data);
- X if (ercode != APM_OK) {
- X return (ercode);
- X }
- X }
- X
- X return (APM_free_mem(apm));
- X}
- X
- Xint
- XAPM_garbage_collect()
- X{
- X int i;
- X int ercode = APM_OK;
- X int code;
- X
- X apm_errno = APM_OK;
- X
- X for (i = 0; i < APM_memnum; ++i) {
- X if (APM_memtab[i].apm != (APM)NULL) {
- X if ((APM_memtab[i].flags & MEMAVAIL) != 0) {
- X code = APM_do_free(APM_memtab[i].apm);
- X APM_memtab[i].apm = (APM)NULL;
- X APM_memtab[i].flags = 0;
- X if (code != APM_OK) {
- X ercode = code;
- X }
- X }
- X }
- X }
- X
- X return (APM_set_errno(ercode));
- X}
- X
- Xint
- XAPM_free(apm)
- XAPM apm;
- X{
- X int i;
- X
- X apm_errno = APM_OK;
- X
- X if (apm == (APM)NULL) {
- X return (APM_set_errno(APM_WNULL));
- X }
- X
- X for (i = 0; i < APM_memnum; ++i) {
- X if (APM_memtab[i].apm == apm) {
- X if ((APM_memtab[i].flags & MEMAVAIL) == 0) {
- X APM_memtab[i].flags |= MEMAVAIL;
- X return (APM_set_errno(APM_OK));
- X }
- X }
- X }
- X
- X return (APM_set_errno(APM_WNOALLOC));
- X}
- @\END_OF_FILE_memory.c
- else
- echo "shar: Will not over write memory.c"
- fi
- if `test ! -s misc.c`
- then
- echo "x - misc.c"
- sed 's/^X//' > misc.c << '@\END_OF_FILE_misc.c'
- X/******************************************************************************
- X
- X Arbitrary Precision Math Library General Public License
- X (Written October 5, 1988)
- X
- X Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
- X Gatos, California. Everyone is permitted to copy and distribute
- X verbatim copies of this license, but changing it is not allowed.
- X You can also use this wording to make the terms for other programs.
- X
- X The wording of this license is based on that of the
- X "GNU EMACS GENERAL PUBLIC LICENSE" by Richard Stallman,
- X Copyright (C) 1985, 1987, 1988, version of February 11, 1988,
- X but since some of the text has been changed, please be sure to
- X READ THIS CAREFULLY!
- X
- X This general public license is intended to give everyone the right
- Xto share the Arbitrary Precision Math Library (hereinafter referred to
- Xas the "APM Library"). To make sure that you get the rights we want
- Xyou to have, I need to make restrictions that forbid anyone to deny
- Xyou these rights or to ask you to surrender the rights.
- X
- X Specifically, we want to make sure that you have the right to give
- Xaway copies of the APM Library, that you receive source code or else
- Xcan get it if you want it, that you can change the APM Library or use
- Xpieces of it in new programs, and that you know you can do these
- Xthings.
- X
- X To make sure that everyone has such rights, we have to forbid you to
- Xdeprive anyone else of these rights. For example, if you distribute
- Xcopies of the APM Library, you must give the recipients all the
- Xrights that you have. You must make sure that they, too, receive or
- Xcan get the source code. And you must tell them their rights.
- X
- X Also, for our own protection, we must make certain that everyone
- Xfinds out that there is no warranty for the APM Library. If the APM
- XLibrary is modified by someone else and passed on, we want its
- Xrecipients to know that what they have is not what we distributed, so
- Xthat any problems introduced by others will not reflect on our
- Xreputation.
- X
- X Therefore we (Lloyd Zusman and Master Byte Software) make the
- Xfollowing terms which say what you must do to be allowed to
- Xdistribute or change the APM Library.
- X
- X COPYING POLICIES
- X
- X1. You may copy and distribute verbatim copies of the APM Library
- Xsource code as you receive it, in any medium, provided that you
- Xconspicuously and appropriately publish on each copy a valid copyright
- Xnotice "Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
- XGatos, California" (or with whatever year is appropriate); keep intact
- Xthe notices on all files that refer to this License Agreement and to
- Xthe absence of any warranty; and give any other recipients of the the
- XAPM Library program a copy of this License Agreement along with the
- Xprogram. You may charge a distribution fee for the physical act of
- Xtransferring a copy.
- X
- X 2. You may modify your copy or copies of the APM Library source code or
- Xany portion of it, and copy and distribute such modifications under
- Xthe terms of Paragraph 1 above, provided that you also do the following:
- X
- X a) cause the modified files to carry prominent notices stating
- X that you changed the files and the date of any change; and
- X
- X b) cause the whole of any work that you distribute or publish, that in
- X whole or in part contains or is a derivative of the APM Library or any
- X part thereof, to be licensed to all third parties on terms identical
- X to those contained in this License Agreement (except that you may
- X choose to grant more extensive warranty protection to some or all
- X third parties, at your option).
- X
- X c) You may charge a distribution fee for the physical act of
- X transferring a copy, and you may at your option offer warranty
- X protection in exchange for a fee.
- X
- X d) You may not charge a license fee for the whole of any work that
- X you distribute or publish, that in whole or in part contains or is
- X a derivative of the APM library or any part thereof, without the
- X express written permission of Lloyd Zusman and Master Byte Software;
- X whether this permission is granted for free or in return for goods
- X services, royalties, or other compensation will be determined
- X solely by Lloyd Zusman and Master Byte Software.
- X
- XMere aggregation of another unrelated program with this program (or its
- Xderivative) on a volume of a storage or distribution medium does not bring
- Xthe other program under the scope of these terms.
- X
- X 3. You may copy and distribute the APM Library (or a portion or
- Xderivative of it, under Paragraph 2) in object code or executable form
- Xunder all the terms of Paragraphs 1 and 2 above provided that you also
- Xdo one of the following:
- X
- X a) accompany it with the complete corresponding machine-readable
- X source code, which must be distributed under the terms of
- X Paragraphs 1 and 2 above; or,
- X
- X b) accompany it with a written offer, valid for at least three
- X years, to give any third party free (except for a nominal
- X shipping charge) a complete machine-readable copy of the
- X corresponding source code, to be distributed under the terms of
- X Paragraphs 1 and 2 above; or,
- X
- X c) accompany it with the information you received as to where the
- X corresponding source code may be obtained. (This alternative is
- X allowed only for noncommercial distribution and only if you
- X received the program in object code or executable form alone.)
- X
- XFor an executable file, complete source code means all the source code
- Xfor all modules it contains; but, as a special exception, it need not
- Xinclude source code for modules which are standard libraries that
- Xaccompany the operating system on which the executable file runs.
- X
- X 4. You may not copy, sublicense, distribute or transfer the APM
- XLibrary except as expressly provided under this License Agreement.
- XAny attempt otherwise to copy, sublicense, distribute or transfer the
- XAPM Library is void and your rights to use the APM Library under this
- XLicense agreement shall be automatically terminated. However, parties
- Xwho have received computer software programs from you with this
- XLicense Agreement will not have their licenses terminated so long as
- Xsuch parties remain in full compliance.
- X
- X 5. If you wish to incorporate parts of the APM Library into other
- Xprograms whose distribution conditions are different, write to Lloyd
- XZusman at Master Byte Software. We have not yet worked out a simple
- Xrule that can be stated here, but we will often permit this. We will
- Xbe guided by the goals of (1) preserving the free status of all
- Xderivatives of our free software; of (2) promoting the sharing and
- Xreuse of software; and of (3) not allowing anyone to profit from the
- Xuse of our software without us also having the opportunity to share
- Xin these profits.
- X
- XYour comments and suggestions about our licensing policies and our
- Xsoftware are welcome! Please contact Lloyd Zusman, Master Byte
- XSoftware, 127 Wilder Ave., Los Gatos, California 95030, or call
- X(408) 395-5693.
- X
- X NO WARRANTY
- X
- X BECAUSE THE APM LIBRARY IS LICENSED FREE OF CHARGE, WE PROVIDE
- XABSOLUTELY NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE
- XLAW. EXCEPT WHEN OTHERWISE STATED IN WRITING, MASTER BYTE SOFTWARE,
- XLLOYD ZUSMAN AND/OR OTHER PARTIES PROVIDE THE APM LIBRARY "AS IS"
- XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
- XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- XFITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
- XAND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE THE APM
- XLIBRARY PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
- XSERVICING, REPAIR OR CORRECTION.
- X
- X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL MASTER BYTE
- XSOFTWARE, LLOYD ZUSMAN, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND
- XREDISTRIBUTE THE APM LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
- XDAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL,
- XINCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
- XINABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA
- XBEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A
- XFAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY
- XMASTER BYTE SOFTWARE) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF
- XTHE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
- X
- X******************************************************************************/
- X
- X
- X/*
- X * Miscellaneous routines for the APM library.
- X *
- X * $Log: misc.c,v $
- X * Revision 1.0 88/10/05 12:38:14 ljz
- X * Initial release.
- X *
- X */
- X#ifndef lint
- Xstatic char rcsid[] = "$Header: misc.c,v 1.0 88/10/05 12:38:14 ljz Exp $";
- X#endif /* ! lint */
- X
- X#include <stdio.h>
- X#include "apm.h"
- X#include "apmlocal.h"
- X
- Xstatic FILE *APM_debug_file = (FILE *)NULL;
- X
- XAPM
- Xapm_init(init, scaleFactor, base)
- Xlong init;
- Xint scaleFactor;
- Xshort base;
- X{
- X APM apm;
- X int ercode;
- X
- X apm_errno = APM_OK;
- X
- X apm = APM_alloc();
- X if (apm == (APM)NULL) {
- X APM_error(APM_ENOMEM);
- X return ((APM)NULL);
- X }
- X
- X ercode = APM_parse_long(apm, init, base);
- X if (ercode < APM_OK) {
- X return ((APM)NULL);
- X }
- X ercode = APM_shift(apm, scaleFactor);
- X if (ercode < APM_OK) {
- X return ((APM)NULL);
- X }
- X return (apm);
- X}
- X
- Xint
- Xapm_dispose(apm)
- XAPM apm;
- X{
- X apm_errno = APM_OK;
- X
- X return (APM_error(APM_free(apm)));
- X}
- X
- Xint
- Xapm_garbage_collect()
- X{
- X apm_errno = APM_OK;
- X
- X return (APM_error(APM_garbage_collect()));
- X}
- X
- Xint
- Xapm_assign(result, value)
- XAPM result;
- XAPM value;
- X{
- X apm_errno = APM_OK;
- X
- X ERR_RETURN(apm_validate(value));
- X
- X if (result == value) {
- X return (APM_OK);
- X }
- X
- X ERR_RETURN(APM_size(result, value->length));
- X
- X APM_copy_bytes(result->data, value->data, value->length * sizeof(short));
- X result->sign = SIGNOF(value->sign);
- X result->base = value->base;
- X result->length = value->length;
- X result->dp = value->dp;
- X return (APM_OK);
- X}
- X
- Xint
- Xapm_assign_long(result, longval, scaleFactor, base)
- XAPM result;
- Xlong longval;
- Xint scaleFactor;
- Xshort base;
- X{
- X static APM localApm = (APM)NULL;
- X
- X apm_errno = APM_OK;
- X
- X if (localApm == (APM)NULL) {
- X localApm = APM_alloc();
- X if (apm_errno < 0) {
- X return (APM_error(APM_ENOMEM));
- X }
- X }
- X ERR_RETURN(APM_parse_long(localApm, longval, base));
- X ERR_RETURN(APM_shift(localApm, scaleFactor));
- X
- X return (APM_error(apm_assign(result, localApm)));
- X}
- X
- Xint
- Xapm_assign_string(result, string, base)
- XAPM result;
- Xchar *string;
- Xshort base;
- X{
- X apm_errno = APM_OK;
- X
- X return (APM_error(APM_parse_string(result, string, base)));
- X}
- X
- Xint
- Xapm_round(result, apm, dp)
- XAPM result;
- XAPM apm;
- Xint dp;
- X{
- X static APM localApm1 = (APM)NULL;
- X static APM localApm2 = (APM)NULL;
- X
- X apm_errno = APM_OK;
- X
- X if (dp < 0) {
- X return (APM_set_errno(APM_EPARM));
- X }
- X
- X ERR_RETURN(APM_val_format(result));
- X ERR_RETURN(APM_val_format(apm));
- X ERR_RETURN(APM_val_base(apm->base));
- X
- X ERR_RETURN(APM_trim(apm, 1, 0));
- X
- X if (dp < 0 || dp >= apm->dp) {
- X return (apm_assign(result, apm));
- X }
- X
- X if (localApm1 == (APM)NULL) {
- X localApm1 = APM_alloc();
- X if (localApm1 == (APM)NULL) {
- X return (APM_error(APM_ENOMEM));
- X }
- X }
- X
- X if (localApm2 == (APM)NULL) {
- X localApm2 = APM_alloc();
- X if (localApm2 == (APM)NULL) {
- X return (APM_error(APM_ENOMEM));
- X }
- X }
- X
- X if (apm->base == SPECIAL_BASE) {
- X ERR_RETURN(apm_assign_long(localApm1, 5L, -dp - 1, 10));
- X ERR_RETURN(apm_assign(localApm2, apm));
- X ERR_RETURN(APM_spec_to_norm(localApm2));
- X ERR_RETURN(apm_add(result, localApm2, localApm1));
- X ERR_RETURN(APM_setdp(localApm2, result, dp));
- X ERR_RETURN(apm_assign(result, localApm2));
- X ERR_RETURN(APM_norm_to_spec(result));
- X }
- X else {
- X ERR_RETURN(apm_assign_long(localApm1, (long)(apm->base / 2),
- X -dp - 1, apm->base));
- X ERR_RETURN(apm_add(localApm2, apm, localApm1));
- X ERR_RETURN(APM_setdp(result, localApm2, dp));
- X }
- X
- X return (APM_set_errno(APM_trim(result, 1, 0)));
- X}
- X
- Xint
- Xapm_convert(result, length, dp, round, leftjust, apm)
- Xchar *result;
- Xint length;
- Xint dp;
- Xint round;
- Xint leftjust;
- XAPM apm;
- X{
- X static APM localApm1 = (APM)NULL;
- X static APM localApm2 = (APM)NULL;
- X static APM recursiveApm = (APM)NULL;
- X static char *localString = NULL;
- X static int localLen = 0;
- X int roffset;
- X int loffset;
- X int stringlen;
- X int dpos = -1;
- X int n;
- X char *string;
- X
- X apm_errno = APM_OK;
- X
- X if (result == NULL) {
- X return (APM_error(APM_ENULLVAL));
- X }
- X *result = '\0';
- X
- X ERR_RETURN(APM_val_format(apm));
- X ERR_RETURN(APM_val_base(apm->base));
- X
- X if (apm->data == NULL && apm->length > 0) {
- X return (APM_error(APM_ENULLVAL));
- X }
- X if (length <= 1) {
- X return (APM_error(APM_EPARM));
- X }
- X
- X if (apm->length < 0) {
- X return (APM_error(APM_EPARM));
- X }
- X
- X if (apm->base == SPECIAL_BASE) {
- X if (recursiveApm == (APM)NULL) {
- X recursiveApm = APM_alloc();
- X if (recursiveApm == (APM)NULL) {
- X return (APM_error(APM_ENOMEM));
- X }
- X }
- X ERR_RETURN(apm_assign(recursiveApm, apm));
- X ERR_RETURN(APM_spec_to_norm(recursiveApm));
- X return (apm_convert(result, length, dp, round, leftjust,
- X recursiveApm));
- X }
- X
- X if (localApm1 == (APM)NULL) {
- X localApm1 = APM_alloc();
- X if (localApm1 == (APM)NULL) {
- X return (APM_error(APM_ENOMEM));
- X }
- X }
- X
- X
- X if (!round || dp < 0) {
- X ERR_RETURN(APM_setdp(localApm1, apm, dp));
- X }
- X else {
- X if (localApm2 == (APM)NULL) {
- X localApm2 = APM_alloc();
- X if (localApm2 == (APM)NULL) {
- X return (APM_error(APM_ENOMEM));
- X }
- X }
- X ERR_RETURN(apm_round(localApm2, apm, dp));
- X ERR_RETURN(APM_setdp(localApm1, localApm2, dp));
- X }
- X
- X ERR_RETURN(APM_trim(localApm1, 1, 0));
- X
- X if (localApm1->dp < 0) {
- X stringlen = localApm1->length - localApm1->dp;
- X }
- X else if (localApm1->length < localApm1->dp) {
- X stringlen = localApm1->dp + 2;
- X }
- X else {
- X stringlen = localApm1->length;
- X if (localApm1->dp > 0) {
- X dpos = localApm1->length - localApm1->dp;
- X ++stringlen;
- X if (dpos == 0) {
- X ++stringlen;
- X }
- X }
- X }
- X if (localApm1->sign < 0) {
- X ++stringlen;
- X }
- X if (stringlen + 1 > localLen) {
- X localString = APM_alloc_mem(localString, stringlen + 1,
- X sizeof (char));
- X if (localString == NULL) {
- X return (APM_error(APM_ENOMEM));
- X }
- X localLen = stringlen + 1;
- X }
- X
- X string = localString;
- X if (localApm1->sign < 0) {
- X *string++ = '-';
- X }
- X if (localApm1->length < localApm1->dp) {
- X *string++ = '0';
- X *string++ = '.';
- X for (n = localApm1->length; n < localApm1->dp; ++n) {
- X *string++ = '0';
- X }
- X }
- X if (dpos == 0) {
- X *string++ = '0';
- X }
- X string = APM_build_string(string, localApm1->data, localApm1->length,
- X dpos);
- X for (n = localApm1->dp; n < 0; ++n) {
- X *string++ = '0';
- X }
- X *string = '\0';
- X
- X if (localString[0] == '\0') {
- X localString[0] = '0';
- X localString[1] = '\0';
- X stringlen = 1;
- X }
- X
- X --length;
- X if (leftjust) {
- X roffset = 0;
- X loffset = 0;
- X if (length > stringlen) {
- X length = stringlen;
- X }
- X }
- X else if (stringlen >= length) {
- X roffset = 0;
- X loffset = stringlen - length;
- X }
- X else {
- X roffset = length - stringlen;
- X loffset = 0;
- X }
- X
- X APM_copy_bytes(result + roffset, localString + loffset, length);
- X result[length] = '\0';
- X while (roffset-- > 0) {
- X result[roffset] = ' ';
- X }
- X
- X if (length < stringlen) {
- X return (APM_error(APM_WTRUNC));
- X }
- X else {
- X return (APM_OK);
- X }
- X}
- X
- Xint
- Xapm_scale(result, num, scaleFactor)
- XAPM result;
- XAPM num;
- Xint scaleFactor;
- X{
- X int ercode;
- X
- X apm_errno = APM_OK;
- X
- X ERR_RETURN(APM_val_format(result));
- X ERR_RETURN(apm_assign(result, num));
- X
- X ercode = APM_shift(result, scaleFactor);
- X return (APM_error(ercode));
- X}
- X
- Xint
- Xapm_compare(num1, num2)
- XAPM num1;
- XAPM num2;
- X{
- X static APM localApm = (APM)NULL;
- X int n;
- X
- X apm_errno = APM_OK;
- X
- X ERR_RETURN(apm_validate(num1));
- X ERR_RETURN(apm_validate(num2));
- X
- X if (num1 == num2) {
- X return (0);
- X }
- X
- X if (num1->base != num2->base) {
- X return (APM_error(APM_EBASE));
- X }
- X
- X if (localApm == (APM)NULL) {
- X localApm = APM_alloc();
- X if (localApm == (APM)NULL) {
- X return (APM_error(APM_ENOMEM));
- X }
- X }
- X
- X ERR_RETURN(apm_subtract(localApm, num1, num2));
- X
- X for (n = 0; n < localApm->length; ++n) {
- X if (localApm->data[n] != 0) {
- X break;
- X }
- X }
- X
- X if (n >= localApm->length) {
- X return (0);
- X }
- X else if (localApm->sign >= 0) {
- X return (1);
- X }
- X else {
- X return (-1);
- X }
- X}
- X
- Xint
- Xapm_compare_long(apm, longval, scaleFactor, base)
- XAPM apm;
- Xlong longval;
- Xint scaleFactor;
- Xshort base;
- X{
- X static APM localApm = (APM)NULL;
- X int comparison;
- X
- X apm_errno = APM_OK;
- X
- X if (localApm == (APM)NULL) {
- X localApm = APM_alloc();
- X if (localApm == (APM)NULL) {
- X return (APM_error(APM_ENOMEM));
- X }
- X }
- X
- X ERR_RETURN(apm_validate(apm));
- X ERR_RETURN(APM_parse_long(localApm, longval, base));
- X ERR_RETURN(APM_shift(localApm, scaleFactor));
- X
- X comparison = apm_compare(apm, localApm);
- X if (apm_errno < 0) {
- X return (APM_error(apm_errno));
- X }
- X return (comparison);
- X}
- X
- Xint
- Xapm_sign(apm)
- XAPM apm;
- X{
- X apm_errno = APM_OK;
- X
- X ERR_RETURN(apm_validate(apm));
- X
- X return ((int)SIGNOF(apm->sign));
- X}
- X
- Xint
- Xapm_absolute_value(result, apm)
- XAPM result;
- XAPM apm;
- X{
- X int sign;
- X
- X apm_errno = APM_OK;
- X
- X ERR_RETURN(APM_val_format(result));
- X
- X if (result == apm) {
- X return (APM_error(APM_EOVERLAP));
- X }
- X
- X sign = apm_sign(apm);
- X if (apm_errno < 0) {
- X return (APM_error(apm_errno));
- X }
- X
- X if (sign < 0) {
- X return (APM_error(apm_negate(result, apm)));
- X }
- X else {
- X return (APM_error(apm_assign(result, apm)));
- X }
- X}
- X
- Xint
- Xapm_negate(result, num)
- XAPM result;
- XAPM num;
- X{
- X apm_errno = APM_OK;
- X
- X ERR_RETURN(APM_val_format(result));
- X
- X if (result == num) {
- X return (APM_error(APM_EOVERLAP));
- X }
- X ERR_RETURN(apm_assign(result, num));
- X
- X result->sign = -(SIGNOF(result->sign));
- X
- X return (APM_OK);
- X}
- X
- Xint
- Xapm_reciprocal(result, max_precision, num)
- XAPM result;
- Xint max_precision;
- XAPM num;
- X{
- X static APM apmOne = (APM)NULL;
- X
- X apm_errno = APM_OK;
- X
- X ERR_RETURN(APM_val_format(result));
- X ERR_RETURN(apm_validate(num));
- X
- X if (result == num) {
- X return (APM_error(APM_EOVERLAP));
- X }
- X
- X if (apmOne == (APM)NULL) {
- X apmOne = apm_init(1L, 0, 0);
- X if (apmOne == (APM)NULL) {
- X return (APM_error(APM_ENOMEM));
- X }
- X }
- X
- X apmOne->base = num->base;
- X return (apm_divide(result, max_precision, (APM)NULL, apmOne, num));
- X}
- X
- Xint
- X(*apm_error_func(newfunc))()
- Xint (*newfunc)();
- X{
- X int (*oldfunc)() = APM_error_func;
- X APM_error_func = newfunc;
- X return (oldfunc);
- X}
- X
- Xint
- Xapm_validate(apm)
- XAPM apm;
- X{
- X apm_errno = APM_OK;
- X
- X ERR_RETURN(APM_val_format(apm));
- X ERR_RETURN(APM_val_base(apm->base));
- X
- X return (APM_OK);
- X}
- X
- XFILE *
- Xapm_debug_file(file)
- XFILE *file;
- X{
- X FILE *old = APM_debug_file;
- X
- X APM_debug_file = file;
- X return (old);
- X}
- @\END_OF_FILE_misc.c
- else
- echo "shar: Will not over write misc.c"
- fi
- echo "Finished archive 3 of 5"
- # to concatenate archives, remove anything after this line
- exit 0
-